<?php
$teamId = $_GET['teamId'];
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="../../lib/layui-v2.8.12/src/layui.js"></script>
  <link rel="stylesheet" href="../../lib/layui-v2.8.12/src/css/layui.css">
  <script src="../../utils/form-serialize.js"></script>
  <script src="../../lib/axios/axios.min.js"></script>
  <link rel="stylesheet" href="../../css/common.css">
  
  <title>Document</title>
</head>

<body>
  <!-- 给容器追加 class="layui-form-pane"，即可显示为方框风格 -->
  <form class="layui-form layui-form-pane" id="add" action="./update.php?teamId=<?php echo $teamId; ?>" method="POST" lay-filter="update">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">球队名</label>
        <div class="layui-input-block">
          <input type="text" name="teamName" maxlength="10" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">世界排名</label>
        <div class="layui-input-inline">
          <input type="text" name="worldRank" pattern="^[0-9]{1,2}$" autocomplete="off" class="layui-input" required>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">教练</label>
        <div class="layui-input-inline">
          <input type="text" name="coach" autocomplete="off" class="layui-input" required>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">胜局</label>
        <div class="layui-input-inline" style="width: 150px;">
          <input type="text" name="wins" pattern="^[0-3]{1}$" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">平局</label>
        <div class="layui-input-inline" style="width: 150px;">
          <input type="text" name="draws" pattern="^[0-3]{1}$" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">败局</label>
        <div class="layui-input-inline" style="width: 150px;">
          <input type="text" name="fails" pattern="^[0-3]{1}$" autocomplete="off" class="layui-input" required>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">进球数</label>
        <div class="layui-input-inline">
          <input type="text" name="goals" pattern="^[0-9]{1,2}$" autocomplete="off" class="layui-input" required>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">当家球星</label>
      <div class="layui-input-block" style="width: 200px;">
        <select id="selectPlayer" name="playerId">
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">夺冠胜率</label>
        <div class="layui-input-inline" style="width: 200px;">
          <input name="winRate" autocomplete="off" class="layui-input" required pattern="^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$">
        </div>
      </div>
    </div>
    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">球队描述</label>
      <div class="layui-input-block">
        <textarea name="teamDesc" class="layui-textarea"></textarea>
      </div>
    </div>

    <div class="layui-form-item btn">
      <div>
        <button class="layui-btn" id="confirm">确认</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>
  <?php
  DEFINE('DB_USER', 'worldcup');
  DEFINE('DB_PASSWORD', '123456');
  DEFINE('DB_HOST', 'localhost');
  DEFINE('DB_NAME', 'worldcup');

  $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  if (!$dbc) {
    die('Could not to connect to Mysql:' . mysqli_connect_error());
  }
  mysqli_set_charset($dbc, 'utf8');

  $teamId = $_GET['teamId'];

  $teamSql = "select * from team where team_id = {$teamId};";
  $teamAaary = mysqli_query($dbc, $teamSql);
  $rows = mysqli_fetch_assoc($teamAaary);

  //下拉框球员选择
  $playerNameSql = "select player_name from player;";
  $playerAaary = mysqli_query($dbc, $playerNameSql);
  // 初始化一个空数组来存储所有行数据
  $dataArray = array();
  // 逐行提取数据并存入数组
  while ($row = mysqli_fetch_assoc($playerAaary)) {
    $dataArray[] = $row['player_name'];
  }
  ?>
  <script>
    //下拉框
    let ans = <?php echo json_encode($dataArray); ?>;
    var select = document.getElementById('selectPlayer');
    for (var i = 1; i <= ans.length; i++) {
      var option = document.createElement("option");
      option.setAttribute("value", i);
      option.innerText = ans[i - 1];
      if (i == ans.length) {
        option.setAttribute("selected", "selected");
      }
      select.appendChild(option)
    }
    playerId = select.value;
    //渲染下拉框
    layui.use('form', function() {
      var form = layui.form;
      form.render("select");
      form.on('select', function(data) {
        playerId = data.value;
      });
    });
    let res = <?php echo json_encode($rows); ?>;
    // console.log(res);
    layui.use(function() {
      var $ = layui.$;
      var form = layui.form;
      // 表单赋值
      form.val('update', {
        "teamName": res.team_name,
        "worldRank": +res.world_rank,
        "coach": res.coach,
        "wins": +res.wins,
        "draws": +res.draws,
        "fails": +res.fails,
        "goals": +res.goals,
        "playerId": +res.best_player_id,
        "winRate": parseFloat(res.win_rate),
        "teamDesc": res.team_desc
      });
    })
  </script>

</body>

</html>